//1.导入vue和vue-router
import Vue from 'vue';
import VueRouter from 'vue-router';
import pathCheckArr from '@/router/pathCheckArr.js';

import MyLogin from '@/components/MyLogin.vue';
import MyHome from "@/components/MyHome.vue";
import MyGoods from '@/components/menus/MyGoods.vue';
import MyOrders from '@/components/menus/MyOrders.vue';
import MyRights from '@/components/menus/MyRights.vue';
import MySettings from '@/components/menus/MySettings.vue';
import MyUsers from '@/components/menus/MyUsers.vue';
import MyUserDetail from "@/components/user/MyUserDetail.vue";

//2.调用vue.use()命令，将vue-router挂载到vue上
Vue.use(VueRouter);

//3.创建路由实例对象
const router = new VueRouter({
    routes:[
        {path:'/',redirect:'/login'},
        {path:'/login',component:MyLogin},
        {
            path:'/home',
            component:MyHome,
            redirect:'/home/users',
            children:[
                {path:'users',component:MyUsers},
                {path:'rights',component:MyRights},
                {path:'goods',component:MyGoods},
                {path:'orders',component:MyOrders},
                {path:'systems',component:MySettings},
                {path:'userInfo/:id',component:MyUserDetail,props:true}
            ]
        }
    ]
});

router.beforeEach((to,from,next)=>{
    if (pathCheckArr.indexOf(to.path)===-1){
        const token= localStorage.getItem("token");
        if(token){
            next();
        }else {
            next('/login');
        }
    } else{
        next();
    }
});

//4.将实例对象共享出去
export default  router;